package com.xt.favorites.admin.service.impl;

import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.ObjectUtil;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xt.favorites.admin.dao.*;
import com.xt.favorites.admin.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.common.utils.PageUtils;
import com.xt.common.utils.Query;

import com.xt.favorites.admin.service.UserService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Resource
    PraiseDao praiseDao;
    @Resource
    NoticeDao noticeDao;
    @Resource
    LookRecordDao lookRecordDao;
    @Resource
    LetterDao letterDao;
    @Resource
    FollowDao followDao;
    @Resource
    FeedbackDao feedbackDao;
    @Resource
    FavoritesDao favoritesDao;
    @Resource
    ConfigDao configDao;
    @Resource
    CommentDao commentDao;
    @Resource
    CollectDao collectDao;
    @Resource
    CollectorViewDao collectorViewDao;

    @Resource
    UserDao userDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        //有条件就拼接
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("id", key).or().like("user_name",key);
        }

        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Integer>> console() {
        List<Map<String, Integer>> data = new ArrayList<Map<String, Integer>>();
        Map<String,Integer> userAll =  userDao.userAll();
        Map<String,Integer> userToday =  userDao.userToday();
        data.add(userAll);
        data.add(userToday);
        return data;
    }

    @Override
    public void removeByIdsAll(List<Long> asList) {
        asList.stream().forEach((item) -> {
            System.out.println(item);
            //通过id查询用户相关联的prairse表数据
            QueryWrapper<PraiseEntity> praiseWrapper = new QueryWrapper<>();
            List<PraiseEntity> praiseList = praiseDao.selectList(praiseWrapper.eq("user_id",item));
            if (praiseList != null) {
                praiseList.stream().forEach((p) ->{
                    praiseDao.deleteById(p);
                });
            }
            //通过id查询用户相关联的notice表数据
            QueryWrapper<NoticeEntity> noticeWrapper = new QueryWrapper<>();
            List<NoticeEntity> noticeEntityList = noticeDao.selectList(noticeWrapper.eq("user_id",item));
            if (noticeEntityList != null) {
                noticeEntityList.stream().forEach((i) ->{
                    noticeDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的lookRecord表数据
            QueryWrapper<LookRecordEntity> lookRecordWrapper = new QueryWrapper<>();
            List<LookRecordEntity> lookRecordEntityList = lookRecordDao.selectList(lookRecordWrapper.eq("user_id",item));
            if (lookRecordEntityList != null) {
                lookRecordEntityList.stream().forEach((i) ->{
                    lookRecordDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的letter表数据
            QueryWrapper<LetterEntity> letterWrapper = new QueryWrapper<>();
            letterWrapper.eq("receive_user_id",item).or().eq("send_user_id",item);
            List<LetterEntity> letterEntityList = letterDao.selectList(letterWrapper);
            if (letterEntityList != null) {
                letterEntityList.stream().forEach((i) ->{
                    letterDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的follow表数据
            QueryWrapper<FollowEntity> followWrapper = new QueryWrapper<>();
            followWrapper.eq("user_id",item);
            List<FollowEntity> followEntityList = followDao.selectList(followWrapper);
            if (followEntityList != null){
                followEntityList.stream().forEach((i) ->{
                    followDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的feedback表数据
            QueryWrapper<FeedbackEntity> feedbackWrapper = new QueryWrapper<>();
            feedbackWrapper.eq("user_id",item);
            List<FeedbackEntity> feedbackEntityList = feedbackDao.selectList(feedbackWrapper);
            if (feedbackEntityList != null) {
                feedbackEntityList.stream().forEach((i) ->{
                    feedbackDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的favorites表数据
            QueryWrapper<FavoritesEntity> favoritesWrapper = new QueryWrapper<>();
            favoritesWrapper.eq("user_id",item);
            List<FavoritesEntity> favoritesList = favoritesDao.selectList(favoritesWrapper);
            if (favoritesList != null) {
                favoritesList.stream().forEach((i) ->{
                    favoritesDao.deleteById(i);
                });

            }
            //通过id查询用户相关联的config表数据
            QueryWrapper<ConfigEntity> configWrapper = new QueryWrapper<>();
            configWrapper.eq("user_id",item);
            List<ConfigEntity> configEntityList = configDao.selectList(configWrapper);
            if (configEntityList != null) {
                configEntityList.stream().forEach((i) ->{
                    configDao.deleteById(i);
                });

            }
            //通过id查询用户相关联的comment表数据
            QueryWrapper<CommentEntity> commentWrapper = new QueryWrapper<>();
            commentWrapper.eq("reply_user_id",item).or().eq("user_id",item);
            List<CommentEntity> commentEntityList = commentDao.selectList(commentWrapper);
            if (commentEntityList != null) {
                commentEntityList.stream().forEach((i) ->{
                    commentDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的CollectorView表数据
            QueryWrapper<CollectorViewEntity> collectorViewWrapper = new QueryWrapper<>();
            List<CollectorViewEntity> collectorViewEntityList = collectorViewDao.selectList(collectorViewWrapper.eq("user_id",item));
            if (collectorViewEntityList != null) {
                collectorViewEntityList.stream().forEach((i) ->{
                    collectorViewDao.deleteById(i);
                });
            }
            //通过id查询用户相关联的Collect表数据
            QueryWrapper<CollectEntity> collectWrapper = new QueryWrapper<>();
            List<CollectEntity> collectEntityList = collectDao.selectList(collectWrapper.eq("user_id",item));
            if (collectEntityList != null) {
                collectEntityList.stream().forEach((i) ->{
                    collectDao.deleteById(i);
                });
            }
            //最后删除该用用户
            userDao.deleteById(item);
        });
    }

    @Override
    public UserEntity findByEmail(String email) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        UserEntity userEntity = userDao.selectOne(wrapper);
        return userEntity;
    }

    @Override
    public UserEntity findByUserName(String userName) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        UserEntity userEntity = userDao.selectOne(wrapper);
        return userEntity;
    }


}